
<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>fofa.client &#8212; FOFA 2.0.0 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/alabaster.css" />
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
   
  <link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for fofa.client</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python3</span>
<span class="c1"># -*- coding: utf-8 -*-</span>
<span class="c1"># Author: Erdog, Loveforkeeps, Alluka</span>
<span class="kn">import</span> <span class="nn">base64</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">from</span> <span class="nn">retry.api</span> <span class="kn">import</span> <span class="n">retry_call</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="c1"># 当作为脚本直接执行时的处理方式</span>
    <span class="kn">from</span> <span class="nn">exception</span> <span class="kn">import</span> <span class="n">FofaError</span>
    <span class="kn">from</span> <span class="nn">helper</span> <span class="kn">import</span> <span class="n">get_language</span><span class="p">,</span> <span class="n">encode_query</span>
<span class="k">else</span><span class="p">:</span>
    <span class="c1"># 当作为包/模块导入时的处理方式</span>
    <span class="kn">from</span> <span class="nn">.exception</span> <span class="kn">import</span> <span class="n">FofaError</span>
    <span class="kn">from</span> <span class="nn">.helper</span> <span class="kn">import</span> <span class="n">get_language</span><span class="p">,</span> <span class="n">encode_query</span>

<div class="viewcode-block" id="Client"><a class="viewcode-back" href="../../fofa.html#fofa.Client">[docs]</a><span class="k">class</span> <span class="nc">Client</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A class representing the FOFA client.</span>

<span class="sd">    :param key: The Fofa api key. If not specified, it will be read from the FOFA_KEY environment variable.</span>
<span class="sd">    :type key: str</span>
<span class="sd">    :param base_url: The base URL of the FOFA API. Defaults to &#39;https://fofa.info&#39;.</span>
<span class="sd">    :type base_url: str</span>
<span class="sd">    :param proxies:  A proxies array for the requests library, e.g. {&#39;https&#39;: &#39;your proxy&#39;}</span>
<span class="sd">    :type proxies: dict</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">base_url</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">proxies</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot; Initialize the FOFA client.</span>
<span class="sd">        &quot;&quot;&quot;</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;FOFA_KEY&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">base_url</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="n">base_url</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;FOFA_BASE_URL&#39;</span><span class="p">,</span> <span class="s1">&#39;https://fofa.info&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="n">key</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">base_url</span> <span class="o">=</span> <span class="n">base_url</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">lang</span> <span class="o">=</span> <span class="s1">&#39;en&#39;</span>
        <span class="n">sys_lang</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">sys_lang</span> <span class="o">!=</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">sys_lang</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;zh&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lang</span> <span class="o">=</span> <span class="s1">&#39;zh-CN&#39;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_session</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">Session</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">proxies</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_session</span><span class="o">.</span><span class="n">proxies</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">proxies</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_session</span><span class="o">.</span><span class="n">trust_env</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="c1"># retry config, in seconds</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tries</span> <span class="o">=</span> <span class="mi">5</span>       <span class="c1"># Number of retry attempts</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">delay</span> <span class="o">=</span> <span class="mi">1</span>       <span class="c1"># Initial delay between retries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_delay</span> <span class="o">=</span> <span class="mi">60</span>  <span class="c1"># Maximum delay between retries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">backoff</span> <span class="o">=</span> <span class="mi">2</span>     <span class="c1"># Backoff factor for exponential backoff</span>

<div class="viewcode-block" id="Client.get_userinfo"><a class="viewcode-back" href="../../fofa.html#fofa.Client.get_userinfo">[docs]</a>    <span class="k">def</span> <span class="nf">get_userinfo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get user info for current user.</span>
<span class="sd">    </span>
<span class="sd">        :return: User information in JSON format.</span>
<span class="sd">        :rtype: dict</span>
<span class="sd">        :raises FofaException: If an error occurs during the API request.</span>
<span class="sd">       </span>
<span class="sd">        :Example:</span>
<span class="sd">        </span>
<span class="sd">        The returned JSON result will be in the following format:</span>
<span class="sd">        </span>
<span class="sd">        .. code-block:: json</span>
<span class="sd">        </span>
<span class="sd">            {</span>
<span class="sd">                &quot;username&quot;: &quot;sample&quot;,</span>
<span class="sd">                &quot;fofacli_ver&quot;: &quot;4.0.3&quot;,</span>
<span class="sd">                &quot;fcoin&quot;: 0,</span>
<span class="sd">                &quot;error&quot;: false,</span>
<span class="sd">                &quot;fofa_server&quot;: true,</span>
<span class="sd">                &quot;avatar&quot;: &quot;https://nosec.org/missing.jpg&quot;,</span>
<span class="sd">                &quot;vip_level&quot;: 0,</span>
<span class="sd">                &quot;is_verified&quot;: false,</span>
<span class="sd">                &quot;message&quot;: &quot;&quot;,</span>
<span class="sd">                &quot;isvip&quot;: false,</span>
<span class="sd">                &quot;email&quot;: &quot;username@sample.net&quot;</span>
<span class="sd">            }</span>

<span class="sd">         &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__do_req</span><span class="p">(</span> <span class="s2">&quot;/api/v1/info/my&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.search"><a class="viewcode-back" href="../../fofa.html#fofa.Client.search">[docs]</a>    <span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query_str</span><span class="p">,</span> <span class="n">page</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">opts</span><span class="o">=</span><span class="p">{}):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Search data in FOFA.</span>

<span class="sd">        :param query_str: The search query string.</span>

<span class="sd">            Example 1:</span>
<span class="sd">                &#39;ip=127.0.0.1&#39;</span>
<span class="sd">    </span>
<span class="sd">            Example 2:</span>
<span class="sd">                &#39;header=&quot;thinkphp&quot; || header=&quot;think_template&quot;&#39;</span>

<span class="sd">        :type query_str: str</span>
<span class="sd">        :param page: Page number. Default is 1.</span>
<span class="sd">        :type page: int</span>
<span class="sd">        :param size: Number of results to be returned in one page. Default is 100.</span>
<span class="sd">        :type size: int</span>
<span class="sd">        :param fields: Comma-separated list of fields to be included in the query result.</span>
<span class="sd">            Example:</span>
<span class="sd">            &#39;ip,city&#39;</span>
<span class="sd">        :type fields: str</span>
<span class="sd">        :param opts: Additional options for the query. This should be a dictionary of key-value pairs.</span>
<span class="sd">        :type opts: dict</span>
<span class="sd">        :return: Query result in JSON format.</span>
<span class="sd">        :rtype: dict</span>
<span class="sd">        </span>
<span class="sd">        .. code-block:: json</span>
<span class="sd">        </span>
<span class="sd">            {</span>
<span class="sd">                &quot;results&quot;: [</span>
<span class="sd">                    [</span>
<span class="sd">                        &quot;111.**.241.**:8111&quot;,</span>
<span class="sd">                        &quot;111.**.241.**&quot;,</span>
<span class="sd">                        &quot;8111&quot;</span>
<span class="sd">                    ],</span>
<span class="sd">                    [</span>
<span class="sd">                        &quot;210.**.181.**&quot;,</span>
<span class="sd">                        &quot;210.**.181.**&quot;,</span>
<span class="sd">                        &quot;80&quot;</span>
<span class="sd">                    ]</span>
<span class="sd">                ],</span>
<span class="sd">                &quot;mode&quot;: &quot;extended&quot;,</span>
<span class="sd">                &quot;error&quot;: false,</span>
<span class="sd">                &quot;query&quot;: &quot;app=\\&quot;网宿科技-公司产品\\&quot;&quot;,</span>
<span class="sd">                &quot;page&quot;: 1,</span>
<span class="sd">                &quot;size&quot;: 2</span>
<span class="sd">            }</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">param</span> <span class="o">=</span> <span class="n">opts</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;qbase64&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">encode_query</span><span class="p">(</span><span class="n">query_str</span><span class="p">)</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;page&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">page</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fields</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;size&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;search &#39;</span><span class="si">%s</span><span class="s2">&#39; page:</span><span class="si">%d</span><span class="s2"> size:</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">query_str</span><span class="p">,</span> <span class="n">page</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__do_req</span><span class="p">(</span><span class="s1">&#39;/api/v1/search/all&#39;</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.can_use_next"><a class="viewcode-back" href="../../fofa.html#fofa.Client.can_use_next">[docs]</a>    <span class="k">def</span> <span class="nf">can_use_next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check if the &quot;search_next&quot; API can be used.</span>

<span class="sd">        :return: True if the &quot;search_next&quot; API can be used, False otherwise.</span>
<span class="sd">        :rtype: bool</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">search_next</span><span class="p">(</span><span class="s1">&#39;bad=query&#39;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">FofaError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="mi">820000</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
            <span class="k">return</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="Client.search_next"><a class="viewcode-back" href="../../fofa.html#fofa.Client.search_next">[docs]</a>    <span class="k">def</span> <span class="nf">search_next</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query_str</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="nb">next</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">full</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">opts</span><span class="o">=</span><span class="p">{}):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Query the next page of search results.</span>

<span class="sd">        :param query_str: The search query string.</span>

<span class="sd">            Example 1:</span>
<span class="sd">                &#39;ip=127.0.0.1&#39;</span>
<span class="sd">    </span>
<span class="sd">            Example 2:</span>
<span class="sd">                &#39;header=&quot;thinkphp&quot; || header=&quot;think_template&quot;&#39;</span>

<span class="sd">        :param fields: The fields to be included in the response.</span>
<span class="sd">            Default: &#39;host,ip,port&#39;</span>
<span class="sd">        :type fields: str</span>

<span class="sd">        :param size: The number of results to be returned per page.</span>
<span class="sd">            Default: 100</span>
<span class="sd">            Maximum: 10,000</span>
<span class="sd">        :type size: int</span>

<span class="sd">        :param next: The ID for pagination.</span>
<span class="sd">            The next value is returned in the response of previous search query.</span>
<span class="sd">            If not provided, the first page of results will be returned.</span>
<span class="sd">        :type next: str</span>

<span class="sd">        :param full: Specify if all data should be searched.</span>
<span class="sd">            Default: False (search within the past year)</span>
<span class="sd">            Set to True to search all data.</span>
<span class="sd">        :type full: bool</span>

<span class="sd">        :param opts: Additional options for the search.</span>
<span class="sd">        :type opts: dict</span>

<span class="sd">        :return: The query result in JSON format.</span>
<span class="sd">        :rtype: dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">param</span> <span class="o">=</span> <span class="n">opts</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;qbase64&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">encode_query</span><span class="p">(</span><span class="n">query_str</span><span class="p">)</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fields</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;size&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;full&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">full</span>
        <span class="k">if</span> <span class="nb">next</span> <span class="ow">and</span> <span class="nb">next</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="n">param</span><span class="p">[</span><span class="s1">&#39;next&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">next</span>

        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;search next for &#39;</span><span class="si">%s</span><span class="s2">&#39; size:</span><span class="si">%d</span><span class="s2">, next:</span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">query_str</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="nb">next</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__do_req</span><span class="p">(</span><span class="s1">&#39;/api/v1/search/next&#39;</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.search_stats"><a class="viewcode-back" href="../../fofa.html#fofa.Client.search_stats">[docs]</a>    <span class="k">def</span> <span class="nf">search_stats</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query_str</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="o">=</span><span class="p">{}):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Query the statistics of the search results.</span>

<span class="sd">        :param query_str: The search query string.</span>

<span class="sd">            Example 1:</span>
<span class="sd">                &#39;ip=127.0.0.1&#39;</span>
<span class="sd">    </span>
<span class="sd">            Example 2:</span>
<span class="sd">                &#39;header=&quot;thinkphp&quot; || header=&quot;think_template&quot;&#39;</span>

<span class="sd">        :type query_str: str</span>

<span class="sd">        :param size: The number of results to be aggregated for each item.</span>
<span class="sd">            Default: 5</span>
<span class="sd">        :type size: int</span>

<span class="sd">        :param fields: The fields to be included in the aggregation.</span>
<span class="sd">            Example: &#39;ip,city&#39;</span>
<span class="sd">        :type fields: str</span>

<span class="sd">        :param opts: Additional options for the search.</span>
<span class="sd">        :type opts: dict</span>

<span class="sd">        :return: query result in json format</span>


<span class="sd">        .. code-block:: json</span>

<span class="sd">            {</span>
<span class="sd">                &quot;distinct&quot;: {</span>
<span class="sd">                    &quot;ip&quot;: 1717,</span>
<span class="sd">                    &quot;title&quot;: 411</span>
<span class="sd">                },</span>
<span class="sd">                &quot;lastupdatetime&quot;: &quot;2022-06-17 13:00:00&quot;,</span>
<span class="sd">                &quot;aggs&quot;: {</span>
<span class="sd">                    &quot;title&quot;: [</span>
<span class="sd">                        {</span>
<span class="sd">                            &quot;count&quot;: 35,</span>
<span class="sd">                            &quot;name&quot;: &quot;百度一下，你就知道&quot;</span>
<span class="sd">                        },</span>
<span class="sd">                        {</span>
<span class="sd">                            &quot;count&quot;: 25,</span>
<span class="sd">                            &quot;name&quot;: &quot;百度网盘-免费云盘丨文件共享软件丨超大容量丨存储安全&quot;</span>
<span class="sd">                        },</span>
<span class="sd">                        {</span>
<span class="sd">                            &quot;count&quot;: 16,</span>
<span class="sd">                            &quot;name&quot;: &quot;百度智能云-登录&quot;</span>
<span class="sd">                        },</span>
<span class="sd">                        {</span>
<span class="sd">                            &quot;count&quot;: 2,</span>
<span class="sd">                            &quot;name&quot;: &quot;百度翻译开放平台&quot;</span>
<span class="sd">                        }</span>
<span class="sd">                    ],</span>
<span class="sd">                    &quot;countries&quot;: []</span>
<span class="sd">                },</span>
<span class="sd">                &quot;error&quot;: false</span>
<span class="sd">            }</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">param</span> <span class="o">=</span> <span class="n">opts</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;qbase64&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">encode_query</span><span class="p">(</span><span class="n">query_str</span><span class="p">)</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;fields&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">fields</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;size&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__do_req</span><span class="p">(</span><span class="s1">&#39;/api/v1/search/stats&#39;</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span></div>

<div class="viewcode-block" id="Client.search_host"><a class="viewcode-back" href="../../fofa.html#fofa.Client.search_host">[docs]</a>    <span class="k">def</span> <span class="nf">search_host</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">,</span> <span class="n">detail</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">opts</span><span class="o">=</span><span class="p">{}):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Search for host information based on the specified IP address or domain.</span>

<span class="sd">        :param host: The IP address or domain of the host to search for.</span>
<span class="sd">        :type host: str</span>
<span class="sd">        :param detail: Optional. Specifies whether to show detailed information. Default is False.</span>
<span class="sd">        :type detail: bool</span>
<span class="sd">        :param opts: Optional. Additional options for the search. Default is an empty dictionary.</span>
<span class="sd">        :type opts: dict</span>
<span class="sd">        :return: The query result in JSON format.</span>
<span class="sd">        :rtype: dict</span>

<span class="sd">        .. code-block:: json</span>

<span class="sd">           {</span>
<span class="sd">                &quot;error&quot;: false,</span>
<span class="sd">                &quot;host&quot;: &quot;78.48.50.249&quot;,</span>
<span class="sd">                &quot;ip&quot;: &quot;78.48.50.249&quot;,</span>
<span class="sd">                &quot;asn&quot;: 6805,</span>
<span class="sd">                &quot;org&quot;: &quot;Telefonica Germany&quot;,</span>
<span class="sd">                &quot;country_name&quot;: &quot;Germany&quot;,</span>
<span class="sd">                &quot;country_code&quot;: &quot;DE&quot;,</span>
<span class="sd">                &quot;protocol&quot;: [</span>
<span class="sd">                    &quot;http&quot;,</span>
<span class="sd">                    &quot;https&quot;</span>
<span class="sd">                ],</span>
<span class="sd">                &quot;port&quot;: [</span>
<span class="sd">                    80,</span>
<span class="sd">                    443</span>
<span class="sd">                ],</span>
<span class="sd">                &quot;category&quot;: [</span>
<span class="sd">                    &quot;CMS&quot;</span>
<span class="sd">                ],</span>
<span class="sd">                &quot;product&quot;: [</span>
<span class="sd">                    &quot;Synology-WebStation&quot;</span>
<span class="sd">                ],</span>
<span class="sd">                &quot;update_time&quot;: &quot;2022-06-11 08:00:00&quot;</span>
<span class="sd">            }</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">param</span> <span class="o">=</span> <span class="n">opts</span>
        <span class="n">param</span><span class="p">[</span><span class="s1">&#39;detail&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">detail</span>

        <span class="n">u</span> <span class="o">=</span> <span class="s1">&#39;/api/v1/host/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">host</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__do_req</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">__do_req</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;get&#39;</span><span class="p">):</span>
        <span class="n">u</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">base_url</span> <span class="o">+</span> <span class="n">path</span>
        <span class="n">data</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">req_param</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">FofaError</span><span class="p">(</span><span class="s2">&quot;Empty fofa api key&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">params</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">req_param</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s2">&quot;key&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span><span class="p">,</span>
                <span class="s2">&quot;lang&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">lang</span><span class="p">,</span>
            <span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">req_param</span> <span class="o">=</span> <span class="n">params</span>
            <span class="n">req_param</span><span class="p">[</span><span class="s1">&#39;key&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span>
            <span class="n">req_param</span><span class="p">[</span><span class="s1">&#39;lang&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lang</span>

        <span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;post&#39;</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">params</span>
            <span class="n">params</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">def</span> <span class="nf">make_request</span><span class="p">():</span>
            <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;Accept-Encoding&quot;</span><span class="p">:</span> <span class="s2">&quot;gzip&quot;</span><span class="p">}</span>
            <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_session</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">u</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="n">method</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">req_param</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">status_code</span> <span class="o">!=</span> <span class="mi">200</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Request failed with status code: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">response</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">retry_call</span><span class="p">(</span><span class="n">make_request</span><span class="p">,</span>
                         <span class="n">tries</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tries</span><span class="p">,</span>
                         <span class="n">delay</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">delay</span><span class="p">,</span>
                         <span class="n">max_delay</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_delay</span><span class="p">,</span>
                         <span class="n">backoff</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">backoff</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
        <span class="k">if</span> <span class="s1">&#39;error&#39;</span> <span class="ow">in</span> <span class="n">data</span> <span class="ow">and</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;error&#39;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="n">FofaError</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;errmsg&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">data</span></div>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">client</span> <span class="o">=</span> <span class="n">Client</span><span class="p">()</span>
    <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">client</span><span class="o">.</span><span class="n">can_use_next</span><span class="p">())</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">client</span><span class="o">.</span><span class="n">get_userinfo</span><span class="p">(),</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">client</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s1">&#39;app=&quot;网宿科技-公司产品&quot;&#39;</span><span class="p">,</span> <span class="n">page</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">client</span><span class="o">.</span><span class="n">search_host</span><span class="p">(</span><span class="s1">&#39;78.48.50.249&#39;</span><span class="p">,</span> <span class="n">detail</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">client</span><span class="o">.</span><span class="n">search_stats</span><span class="p">(</span><span class="s1">&#39;domain=&quot;baidu.com&quot;&#39;</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="s1">&#39;title&#39;</span><span class="p">),</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../index.html">FOFA</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../fofa.html">FOFA API</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../index.html">Documentation overview</a><ul>
  <li><a href="../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023 Fofa, Inc.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 6.2.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>